import { useRouter } from "next/navigation";
import { QueryClient, useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { AuthService } from "@/shared/api/api.auth";

export function useRegisterForm() {
    const router = useRouter();
    const { register, handleSubmit } = useForm<{
        email: string;
        password: string;
    }>();
    const queryClient = new QueryClient

    const signInMutation = useMutation(
        {
        mutationFn: AuthService.register,
        onSuccess() {
          router.push('/dashboard');
        }
      },queryClient
      );

    const errorMessage = signInMutation.error?.message;

    return {
        register,
        errorMessage,
        handleSubmit: handleSubmit((data) => signInMutation.mutate(data)),
    };
}